{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.下载weibo.xml(sample.xml代替)语料，利用正则表达式和词典：\n",
    "- 找出发博最多的人\n",
    "- 被转发最多的人\n",
    "- at别人最多的人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "info = {}\n",
    "with open('data/weibo.xml', 'r') as f:\n",
    "    text = f.read()\n",
    "    for item in text.split('<RECORD>')[1:]:\n",
    "        pid_ret = re.search(r'<person_id>.+</person_id>', item)\n",
    "        article_ret = re.search(r'<article>.+</article>', item)\n",
    "        trans_ret = re.search(r'<transmit>.+</transmit>', item)\n",
    "        if not (pid_ret and article_ret and trans_ret):\n",
    "            continue\n",
    "        pid = pid_ret.group()[11:-12]\n",
    "        article = article_ret.group()[9:-10]\n",
    "        trans = trans_ret.group()[10:-11]\n",
    "        if info.get(pid):\n",
    "            info[pid]['post'] += 1\n",
    "            info[pid]['trans'] += int(trans)\n",
    "            info[pid]['at'] += article.count('@')\n",
    "        else:\n",
    "            info[pid] = {\n",
    "                'post': 1,\n",
    "                'trans': int(trans),\n",
    "                'at': article.count('@'),\n",
    "            }\n",
    "# print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1646051850', {'post': 12029, 'trans': 0, 'at': 288}]\n",
      "['1189591617', {'post': 144, 'trans': 767302, 'at': 12}]\n",
      "['1195403385', {'post': 6220, 'trans': 0, 'at': 1963}]\n"
     ]
    }
   ],
   "source": [
    "info_arr = []\n",
    "for key in info.keys():\n",
    "    info_arr.append([key, info[key]])\n",
    "print(sorted(info_arr, key=lambda item: item[1]['post'], reverse=True)[0])\n",
    "print(sorted(info_arr, key=lambda item: item[1]['trans'], reverse=True)[0])\n",
    "print(sorted(info_arr, key=lambda item: item[1]['at'], reverse=True)[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'abc@d@c'.count('@')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.下载《中华人民共和国宪法》(xianfa.txt)，利用jieba进行分词和词性标注。\n",
    "找出动词（v）后面有多于两个词的情况，并按照从高到低输出词性组合的频次表。如：\n",
    "```\n",
    "v a n: 1000\n",
    "v a d: 959\n",
    "……\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "lines = []\n",
    "with open('data/xianfa.txt', 'r', encoding='gbk') as f:\n",
    "    for l in f:\n",
    "        lines.append(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba.posseg as pseg\n",
    "\n",
    "d = {}\n",
    "key = ''\n",
    "v_mode = False\n",
    "for l in lines:\n",
    "    for p in pseg.cut(l):\n",
    "        pos = list(p)[1]\n",
    "        if pos == 'v':\n",
    "            if v_mode and len(key) != 0:\n",
    "                key = ''\n",
    "            v_mode = True\n",
    "            key += 'v '\n",
    "        elif v_mode:\n",
    "            key += pos + ' '\n",
    "            if len(key.split(' ')) == 4:\n",
    "                if d.get(key):\n",
    "                    d[key] += 1\n",
    "                else:\n",
    "                    d[key] = 1\n",
    "#                 print(key)\n",
    "                v_mode = False\n",
    "                key = ''\n",
    "# print(d)\n",
    "\n",
    "arr = []\n",
    "for key in d.keys():\n",
    "    arr.append([key, d[key]])\n",
    "#     print(key, d[key])\n",
    "\n",
    "for i in sorted(arr, key=lambda item: item[1], reverse=True):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'ab', 'c', '']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'a ab c '.split(' ')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.如果人人财富平等，会建成一个平等社会吗？\n",
    "分析：\n",
    "- 假设人人财富数额一致（100元），\n",
    "- 每人每天和任意其他人进行一次交易，\n",
    "- 所有交易都是A付出1元，B收入1元\n",
    "- N（如1000）天后收入还均等吗？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[112, 101, 109, 157, 84, 107, 85, 38, 114, 93]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "people_money = []\n",
    "for _ in range(10):\n",
    "    people_money.append(100)\n",
    "for _ in range(1000):\n",
    "    for i in range(len(people_money)):\n",
    "        random_index = random.randint(0, len(people_money)-1)\n",
    "        people_money[i] -= 1\n",
    "        people_money[random_index] += 1\n",
    "#         print(i, random_index)\n",
    "print(people_money)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
